﻿using Lidgren.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Paintball.Network
{
    public abstract class ClientAgent : NetworkAgent
    {
        //Fields
        public NetClient Client
        {
            get
            {
                return Peer<NetClient>();
            }
        }

        //Construct
        public ClientAgent()
            : base(NetworkAgentMode.Client)
        {

        }

        
        //Methods
        protected void DefaultDiscoveryRequestResponseHandler(NetIncomingMessage inc)
        {
            //We received a response saying, there is a server here
            //out default will be to connect to it
            Connection = Client.Connect(inc.SenderEndPoint);
        }

        public void SendDiscoverMessage()
        {
            Client.DiscoverLocalPeers(SafePort);
        }
        public void SendDiscoverMessage(int port)
        {
            Client.DiscoverLocalPeers(port);
        }
        public void SendDiscoverMessage(string host, int port)
        {
            Client.DiscoverKnownPeer(host, port);
        }
        
        public void Send(IDataPacket packet)
        {
            NetOutgoingMessage msg = Client.CreateMessage();
            msg.Write((byte)1); //1 indicates IDataPacket
            packet.Encode(msg);
            Client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
        }
        public void Send(NetOutgoingMessage msg)
        {
            NetOutgoingMessage trueMsg = Client.CreateMessage();
            trueMsg.Write((byte)0);
            trueMsg.Write(msg.Data);
            Client.SendMessage(trueMsg, NetDeliveryMethod.ReliableUnordered);
        }
    }

    
}
